package com.hzit.servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.entity.Student;
import com.hzit.service.StudentService;
import com.hzit.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static net.sf.jsqlparser.parser.feature.Feature.delete;
import static net.sf.jsqlparser.parser.feature.Feature.update;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService = null;
	@Override
	public void init() throws ServletException {
		//1. 从servlet容器中取得spring容器
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		//2.  spring容器中获得bean
		studentService = wac.getBean(StudentService.class);
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//0. 设置响应对象的字符集编码
		resp.setContentType("text/html;charset=utf8");
		//1. 得到请求参数cmd
		String cmd = req.getParameter("cmd");
		//2. 根据参数的值调用不同的方法
		if(StrUtil.isNotBlank(cmd)){
			if("list".equals(cmd)){
				list(req,resp);
			}
			if("findByPage".equals(cmd)){
				findByPage(req,resp);
			}
			if("search".equals(cmd)){
				search(req,resp);
			}
			if("add".equals(cmd)){
				add(req,resp);
			}
			if("update".equals(cmd)){
				update(req,resp);
			}
			if("delete".equals(cmd)){
				delete(req,resp);
			}
		}
	}


	// 列表学生
	private void list(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		//1. 查询学生列表
		List<Student> students = studentService.findAll();
		//2. 定义R对象
		R<List<Student>> r = R.ok(students);
		//3. 转换为字符串
		String s = JSON.toJSONString(r);
		resp.getWriter().println(s);
		resp.getWriter().flush();
	}
	// 分页查询学生列表
	private void findByPage(HttpServletRequest req, HttpServletResponse resp)   throws ServletException, IOException {
		//1. 得到当前页
		String pageStr = req.getParameter("page");
		String pageSizeStr = req.getParameter("pageSize");
		//2. 转换为当前页与每页大小
		int page = new Integer(pageStr);
		int pageSize = Integer.parseInt(pageSizeStr);
		//3. 开始查询
		PageResult<StudentDTO> ps = studentService.findByPage(page,pageSize);
		//4. 定义R对象
		R<PageResult<StudentDTO>> r = R.ok(ps);
		//5. 转换为字符串
		String s = JSON.toJSONString(r);
		resp.getWriter().println(s);
		resp.getWriter().flush();
	}
	// 分页带条件查询
	private void search(HttpServletRequest req, HttpServletResponse resp)   throws ServletException, IOException {
		//1. 得到当前页
		String pageStr = req.getParameter("page");
		String pageSizeStr = req.getParameter("pageSize");
		//2. 得到分页查询条件
		String stud = req.getParameter("stud");
		//2.1 处理乱码
		stud = new String(stud.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
		//3. 转换为vo对象(vo： 从前端向控制层传送数据)
		StudentVo vo = JSON.parseObject(stud, StudentVo.class);
		//4. 转换为当前页与每页大小
		int page = new Integer(pageStr);
		int pageSize = Integer.parseInt(pageSizeStr);
		//5. 开始查询
		PageResult<StudentDTO> ps = studentService.search(page,pageSize,vo);
		//6. 定义R对象
		R<PageResult<StudentDTO>> r = R.ok(ps);
		//7. 转换为字符串
		String s = JSON.toJSONString(r);
		resp.getWriter().println(s);
		resp.getWriter().flush();
	}
	// 添加学生
	private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 得到要添加的学生对象
		String studStr = req.getParameter("stud");
		//2. 处理中文乱码
		studStr = new String(studStr.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
		//2. 将上面的学生字符串转换为学生对象
		if(StrUtil.isNotBlank(studStr)){
			R r = null;
			try {
				//2.1 转换为学生Vo对象
				Student student = JSON.parseObject(studStr, Student.class);
				//2.2 将学生添加到数据库中
				studentService.save(student);
				//2.3 保存返回R对象
				r = R.ok("保存学生成功！");
			} catch (Exception e) {
				e.printStackTrace();
				r = R.fail("保存学生失败！");
			}finally {
				String s = JSON.toJSONString(r);
				resp.getWriter().println(s);
				resp.getWriter().flush();
			}
		}
	}
	// 修改学生
	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 得到要添加的学生对象
		String studStr = req.getParameter("stud");
		//2. 处理中文乱码
		studStr = new String(studStr.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
		//2. 将上面的学生字符串转换为学生对象
		if(StrUtil.isNotBlank(studStr)){
			R r = null;
			try {
				//2.1 转换为学生Vo对象
				Student student = JSON.parseObject(studStr, Student.class);
				//2.2 将学生修改到数据库中
				studentService.update(student);
				//2.3 保存返回R对象
				r = R.ok("修改学生成功！");
			} catch (Exception e) {
				e.printStackTrace();
				r = R.fail("修改学生失败！");
			}finally {
				String s = JSON.toJSONString(r);
				resp.getWriter().println(s);
				resp.getWriter().flush();
			}
		}
	}
	// 删除学生
	private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 得到要删除的学生编号
		String sid = req.getParameter("sid");
		//2. 定义返回的R对象
		R r = null;
		try {
			//2. 开始删除学生
			studentService.delete(sid);
			//3. 返回R对象
			r = R.ok("删除学生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			r = R.fail("删除学生失败！");
		} finally {
			String s = JSON.toJSONString(r);
			resp.getWriter().println(s);
			resp.getWriter().flush();
		}

	}

}
